# -*- coding: utf-8 -*-
# @Author: Cursor
# @Date: 2025-01-07
# @Last Modified by: Tim Liu
# @Last Modified time: 2024-01-07

# @Description: Ontology table  

from sqlalchemy import Column, String, Integer, ForeignKey, Text
from sqlalchemy.orm import relationship, Mapped, mapped_column
from db.db_base import Base, BaseModel
from apps.vadmin.auth.models import VadminUser

class OntologyDB(BaseModel):
    __tablename__ = 'crewplus_ontology'
    __table_args__ = {'comment': 'Ontology definitions'}

    name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True, comment="Ontology name")
    description: Mapped[str] = mapped_column(Text, nullable=True, comment="Ontology description")
    
    create_user_id: Mapped[int] = mapped_column(
        Integer,
        ForeignKey("vadmin_auth_user.id", ondelete='RESTRICT'),
        comment="Creator"
    )
    create_user: Mapped[VadminUser] = relationship(foreign_keys=create_user_id)

    schemas: Mapped[list["OSchemaDB"]] = relationship(
        "OSchemaDB",
        secondary="crewplus_ontology_schema_association",
        primaryjoin="OntologyDB.id == crewplus_ontology_schema_association.c.ontology_id",
        secondaryjoin="OSchemaDB.id == crewplus_ontology_schema_association.c.schema_id",
        back_populates="ontologies"
    )

# Deferred import to avoid circular dependency
from .ontology_schema_association import ontology_schema_association
from .oschema import OSchemaDB
